package com.shoppingmall.fragment;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.shoppingmall.R;
import com.shoppingmall.adapter.HomeGoodAdepter;
import com.shoppingmall.bean.GoodBean;
import com.shoppingmall.bean.HomeClassBean;
import com.shoppingmall.data.DataApi;
import com.shoppingmall.mainfragment.HomeFragment;
import com.shoppingmall.widget.RecycleGridDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * liangwanbo
 */

public class HomeGoodFragment extends BaseFragment implements HomeFragment.OnModifyListener {
    @BindView(R.id.tv_totle)
    TextView tvTotle;
    @BindView(R.id.tv_salenumber)
    TextView tvSalenumber;
    @BindView(R.id.jiage)
    TextView jiage;
    @BindView(R.id.re_salemoney)
    RelativeLayout reSalemoney;
    @BindView(R.id.lin_tab)
    LinearLayout linTab;
    @BindView(R.id.good_receyview)
    RecyclerView goodReceyview;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;
    private HomeGoodAdepter homeGoodAdepter;
    private String type;
    private int page=1;
    private BaseQuickAdapter<GoodBean.DataBean, BaseViewHolder> mAdapter;
    private List<GoodBean.DataBean> list=new ArrayList<>();
    public HomeGoodFragment(String type) {
        this.type = type;
    }

    @Override
    protected void initView(View rootView) {

    }

    @Override
    protected void initData() {
        Log.e("type",type+"");
        getclassdata(page+"",10+"","",type,"","","","","","","","","","");
        mAdapter=new BaseQuickAdapter<GoodBean.DataBean, BaseViewHolder>(R.layout.item_goods, list) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, GoodBean.DataBean dataBean) {

            }
        };

        goodReceyview.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        goodReceyview.addItemDecoration(new RecycleGridDivider());
        goodReceyview.setAdapter(mAdapter);
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page += page;
                getclassdata(page+"",10+"","",type,"","","","","","","","","","");
            }
        });

    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_home_good;
    }

    @Override
    public void modify(String type) {

    }


    /**
     * 获取分类商品
     */
    public void getclassdata(String page_no,
                             String page_size,
                             String keyword,
                             String category,
                             String brand,
                             String seller_id,
                             String goods_type,
                             String is_raffle,
                             String is_rake_back,
                             String place_delivery,
                             String price_small,
                             String price_big,
                             String sort,
                             String shop_cat_id){
        RxHttpUtils.createApi(DataApi.class)
                .searchGood(page_no,page_size,keyword,category,brand,seller_id,goods_type,is_raffle,is_rake_back,place_delivery,price_small,price_big,sort,shop_cat_id)
                .compose(Transformer.<String>switchSchedulers())
                .subscribe(new CommonObserver<String>() {
                    @Override
                    protected void onError(String errorMsg) {
                        Log.e("goodinfoerror",errorMsg+"");
                    }

                    @Override
                    protected void onSuccess(String goodBean) {
                        Log.e("goodinfo",goodBean+"");
                       /* for(int i=0;i<goodBean.getData().size();i++){
                            list.add(goodBean.getData().get(i));
                        }*/
                    }
                });


    }



}
